package com.jzo2o.foundations.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-16
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {


    @Resource
    private IServeItemService serveItemService;
    @Resource
    private RegionMapper regionMapper;

    /**
     * 根据区域id   分页查询该区域下的服务列表
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                () -> baseMapper.queryServeListByRegionId(dto.getRegionId()));
    }

    /**
     * 批量新增
     * @param serveUpsertReqDTOList
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        if (CollUtils.isEmpty(serveUpsertReqDTOList)){
            throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");

        }
        //1.循环遍历，添加区域服务
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            //1.检验服务项是否为启用状态，不是启用状态不能新增
            ServeItem serveItem = serveItemService.getById(serveUpsertReqDTO.getServeItemId());
            //如果服务项信息不存在或未启用
            if (ObjectUtil.isNull(serveItem)  || (serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus())){
                throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");
            }
            //2.校验是否重复新增
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();
            if(count>0){
                throw new ForbiddenOperationException(serveItem.getName()+"服务已存在");
            }
            //3.新增服务
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }


    }

    /**
     * 服务上架
     * @param id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = this.getById(id);
        //校验服务是否存在 或 草稿或下架状态方可上架
        if (ObjectUtil.isNull(serve) || !(serve.getSaleStatus()==FoundationStatusEnum.INIT.getStatus() || serve.getSaleStatus()==FoundationStatusEnum.DISABLE.getStatus())){
            throw new ForbiddenOperationException("该服务不存在或未上架");
        }
        ServeItem serveItem = serveItemService.getById(serve.getServeItemId());
        //服务项是否存在或启动
        if (ObjectUtil.isNull(serveItem) || serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("该服务项不存在或未启用");
        }

        boolean update = this.lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id)
                .update();
        if (!update){
            throw new ForbiddenOperationException("服务上架失败");
        }
    }

    // 修改价格
    @Override
    @Transactional
    public void updatePrice(Long id, Double price) {
        Serve serve = this.getById(id);
        if (ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("该服务不存在");
        }
        boolean update = lambdaUpdate().eq(Serve::getId, id).set(Serve::getPrice, price).update();
        if (!update){
            throw new ForbiddenOperationException("修改价格失败");
        }

    }

    // 删除服务
    @Override
    public void deleteById(Long id) {
        //1.确定该服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("该服务不存在");
        }
        //
        if (!(serve.getSaleStatus()==FoundationStatusEnum.INIT.getStatus())){
            throw new ForbiddenOperationException("草稿状态才可以删除");
        }
        //获得服务区域
        Region region = regionMapper.selectById(serve.getRegionId());
        if (ObjectUtil.isNull(region)){
            throw new ForbiddenOperationException("该服务区域不存在");
        }


        if (!this.removeById(id)){
            throw new ForbiddenOperationException("删除服务失败");
        }




    }


}
